package com.gonghui.videomanage;

import android.animation.Animator;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class FullScreenActivity extends Activity implements TextureView.SurfaceTextureListener, View.OnClickListener, Animator.AnimatorListener, SeekBar.OnSeekBarChangeListener {
    private MediaPlayer mediaPlayer;
    private TextureView textureView;
    private ProgressBar progressBar;
    private ImageView iv_btn_play, iv_btn_screen;
    private SeekBar sb_progress;
    private TextView tv_cur_time, tv_all_time;
    private RelativeLayout rl_media_layout;
    private LinearLayout ll_play_ctrl;
    private ViewPropertyAnimator animate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen);

        mediaPlayer = VideoManage.getInstance().getMediaPlayer();

        textureView = (TextureView) findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        rl_media_layout = (RelativeLayout) findViewById(R.id.rl_media_layout);
        rl_media_layout.setOnClickListener(this);
        ll_play_ctrl = (LinearLayout) findViewById(R.id.ll_play_ctrl);
        animate = ll_play_ctrl.animate();
        animate.setListener(this);
        iv_btn_play = (ImageView) findViewById(R.id.iv_btn_play);
        iv_btn_play.setOnClickListener(this);
        tv_cur_time = (TextView) findViewById(R.id.tv_cur_time);
        sb_progress = (SeekBar) findViewById(R.id.sb_progress);
        sb_progress.setOnSeekBarChangeListener(this);
        sb_progress.setMax(mediaPlayer.getDuration());
        tv_all_time = (TextView) findViewById(R.id.tv_all_time);
        tv_all_time.setText(formatTime(mediaPlayer.getDuration() / 1000));
        iv_btn_screen = (ImageView) findViewById(R.id.iv_btn_screen);
        iv_btn_screen.setOnClickListener(this);

        rl_media_layout.setOnTouchListener(new View.OnTouchListener() {
            private float start_x;
            private int seek_x;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        start_x = motionEvent.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        seek_x = (int) (motionEvent.getX()-start_x);
                        break;
                    case MotionEvent.ACTION_UP:
                        int seek = mediaPlayer.getCurrentPosition()+seek_x*10;
                        if (seek<0)
                            seek = 0;
                        mediaPlayer.seekTo(seek);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        mediaPlayer.setSurface(new Surface(surfaceTexture));
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        sb_progress.setProgress(mediaPlayer.getCurrentPosition());
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_media_layout) {
            if (ll_play_ctrl.getAlpha() == 0)
                upCtrl();
            else
                downCtrl();
        }
        if (view.getId() == R.id.iv_btn_play) {
            if (mediaPlayer.isPlaying()) {
                VideoManage.getInstance().pause();
                iv_btn_play.setImageResource(R.mipmap.start);
            } else {
                VideoManage.getInstance().start();
                iv_btn_play.setImageResource(R.mipmap.pause);
            }
        }
        if (view.getId() == R.id.iv_btn_screen) {
            finish();
        }
    }

    @Override
    public void onAnimationStart(Animator animator) {

    }

    @Override
    public void onAnimationEnd(Animator animator) {
        if (ll_play_ctrl.getAlpha() == 0)
            ll_play_ctrl.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser)
            mediaPlayer.seekTo(progress);

        tv_cur_time.setText(formatTime(progress / 1000));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mediaPlayer.isPlaying()){
            iv_btn_play.setImageResource(R.mipmap.pause);
        }else {
            iv_btn_play.setImageResource(R.mipmap.start);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VideoManage.getInstance().onFullScreenResult();
    }

    private void upCtrl() {
        ll_play_ctrl.setVisibility(View.VISIBLE);
        animate.alpha(1);
        animate.start();
    }

    private void downCtrl() {
        animate.alpha(0);
        animate.start();
    }

    private String formatTime(int time) {
        String minute = time / 60 > 9 ? "" + time / 60 : "0" + time / 60;
        String second = time % 60 > 9 ? "" + time % 60 : "0" + time % 60;
        return minute + ":" + second;
    }
}
